/************************************************************************************
* CollectorDelegate.cpp                                                             *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "CollectorDelegate.h"

#include "widgets/StarWidget.h"
#include "Collector.h"
#include "common/Utils.h"
#include <QPainter>
#include <QApplication>

CollectorDelegate::CollectorDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}



void CollectorDelegate::paint(QPainter *painter,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index) const {
    if(index.column() != 4) {
        painter->setBackgroundMode(Qt::TransparentMode);
        QStyledItemDelegate::paint(painter, option, index);
    }
    else {
        painter->setBackgroundMode(Qt::OpaqueMode);
        QRect selectedRect = option.rect;
        if(option.state & QStyle::State_Selected) {
            painter->fillRect(selectedRect, QBrush(QColor(0x56, 0xA5, 0xEC)));
        }
        else if(option.state & QStyle::State_MouseOver) {
            painter->fillRect(selectedRect, QBrush(QColor(0xB4, 0xCF, 0xEC)));
        }
        MediaRating rating = Utils::getMediaRating(index.data().toInt());
        StarWidget::paint(rating, painter, option.rect.x(), option.rect.y());
    }
}



QWidget* CollectorDelegate::createEditor(QWidget *parent,
                                     const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const {
    if(index.column() == 4) {
        StarWidget* star = new StarWidget(None, parent);
        return star;
    }
    else {
        QStyledItemDelegate::createEditor(parent, option, index);
    }
}



void CollectorDelegate::setEditorData(QWidget *editor,
                                      const QModelIndex &index) const {
    if(index.column() == 4) {
        MediaRating rating = Utils::getMediaRating(index.data().toInt());
        StarWidget* widget = qobject_cast<StarWidget*>(editor);
        widget->setRating(rating);
    }
    else {
        QStyledItemDelegate::setEditorData(editor, index);
    }
}


void CollectorDelegate::setModelData(QWidget *editor,
                                     QAbstractItemModel *model,
                                     const QModelIndex &index) const {
    if(index.column() == 4) {
        StarWidget* widget = qobject_cast<StarWidget*>(editor);
        model->setData(index, widget->floatingRating());
    }
    else {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}
